package com.example.springboot.config;

import com.example.springboot.bean.Pet;
import com.example.springboot.config.拦截器.RedisUrlInterceptor;
import com.example.springboot.config.拦截器.防登录;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Configuration(proxyBeanMethods = false)//表示是一个配置类  默认true(单实例)
public class MyWebMvcConfigurer implements WebMvcConfigurer {

    @Override//开启矩阵变量1
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        //设置不移除分号  后面的值
        urlPathHelper.setRemoveSemicolonContent(false);//默认是true
        configurer.setUrlPathHelper(urlPathHelper);
    }
    /*
    //开启矩阵变量02
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {

                UrlPathHelper urlPathHelper=new UrlPathHelper();
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }
        };
    }
    */  //开启矩阵变量2

    /**
     * 前端发给后端的都是字符串类型 ，内部封装了124种类型转换器
     * 我们可以自定义添加自己的类型转换器
     */
    @Override //添加自定义转换器  可以用 Lambda表达式
    public void addFormatters(FormatterRegistry registry){
        registry.addConverter(new Converter<String, Pet>() {
            @Override
            public Pet convert(String source) {
//                        source=source.trim();
                if(!source.isEmpty()){
                    Pet pet = new Pet();
                    String[] pets = source.split(",");
                    pet.setName(pets[0]);
                    pet.setWeight(Double.valueOf(pets[1]));
                    return pet;
                }
                return null;
            }
        });

    }

    @Override //这个是重写   重写内容协商有哪些 应该要把原来的
    // 基于请求头的内容协商   基于参数的内容协商 (可以自定义内容协商)  放进去  才能用
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        Map<String, MediaType> mediaTypes = new HashMap<>();
        mediaTypes.put("xml",MediaType.APPLICATION_XML);
        mediaTypes.put("json",MediaType.APPLICATION_JSON);
        mediaTypes.put("x-long",MediaType.parseMediaType("application/x-long"));

        //基于参数的内容协商
        ParameterContentNegotiationStrategy parameterStrategy =
                new ParameterContentNegotiationStrategy(mediaTypes);
        parameterStrategy.setParameterName("format");//可以自定义参数的key

        //基于请求头的内容协商
        HeaderContentNegotiationStrategy contentNegotiationStrategy =
                new HeaderContentNegotiationStrategy();//适配头

        //将内容协商放进去  否则会找不到  还可以自定义基于路径的内容协商等等
        configurer.strategies(Arrays.asList(parameterStrategy,contentNegotiationStrategy));

    }
    //这个是重写

    @Autowired
    RedisUrlInterceptor redisUrlInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new 防登录())//添加拦截规则

                //需要拦截哪些 /**是所有请求都拦截  "包括静态"
                .addPathPatterns("/table","/main.html","/main","/filesc","/userLongs","/logins")
                .excludePathPatterns("/","/login","");//需要放行哪些

        registry.addInterceptor(redisUrlInterceptor)
                .addPathPatterns("/**")//需要拦截哪些 /**是所有请求都拦截  包括静态
                .excludePathPatterns("/","/login","/css/**",
                        "/js/**","/fonts/**","/images/**");//需要放行哪些
    }//配置拦截器


}
